package com.valco.datacontrol.openapi;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.valco.data.Book;

public class OpenAPIServiceNaver implements OpenAPIService {

	public OpenAPIServiceNaver() {
		
	}

	public static String getContent(Element element, String tagName) {
		NodeList list = element.getElementsByTagName(tagName);
		Element cElement = (Element) list.item(0);

		if (cElement.getFirstChild() != null) {
			return cElement.getFirstChild().getNodeValue();
		} else {
			return "";
		}
	}

	@Override
	public ArrayList<Book> SearchBook(RequestQueryData requestQueryData, OpenAPIXMLParser parser) {
		String uri = null;

		try {
			uri = "http://openapi.naver.com/search?key=" + requestQueryData.getApiKey() + "&target=" + requestQueryData.getTarget() + "&query="
					+ URLEncoder.encode(requestQueryData.getQuery(), "UTF-8");

			ArrayList<Book> listItem = parser.parseToData(requestQueryData, uri);

			return listItem;
		} catch (UnsupportedEncodingException e) {
			System.out.println(e);
		}
		return null;
	}

}
